package com.smart.parking.service.impl;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.parking.entity.ChargeEntity;
import com.smart.parking.entity.VehicleEntity;
import com.smart.parking.mapper.ChargeMapper;
import com.smart.parking.param.AliPay;
import com.smart.parking.service.ChargeService;
import com.smart.parking.service.PriceService;
import com.smart.parking.service.VehicleService;
import com.smart.parking.utils.PageUtils;
import com.smart.parking.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.Map;


@Service("chargeService")
public class ChargeServiceImpl extends ServiceImpl<ChargeMapper, ChargeEntity> implements ChargeService {

    @Autowired
    private PriceService priceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ChargeEntity> page = this.page(
                new Query<ChargeEntity>().getPage(params),
                new QueryWrapper<ChargeEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 添加费用
     *
     * @param vehicleEntity 车辆实体
     * @return boolean
     */
    @Override
    public boolean addCharge(VehicleEntity vehicleEntity) {
        ChargeEntity chargeEntity = new ChargeEntity();
        chargeEntity.setVehiclePlate(vehicleEntity.getVehiclePlate());
        int insert = baseMapper.insert(chargeEntity);
        return insert > 0;
    }

    /**
     * 计算价格
     *
     * @param vehicleEntity 车辆实体
     * @return double
     */
    @Override
    public AliPay calculationPrice(VehicleEntity vehicleEntity) {
        ChargeEntity chargeEntity = baseMapper.selectOne(new QueryWrapper<ChargeEntity>().eq("vehicle_id", vehicleEntity.getVehicleId()));
        Date date = new Date();
        //获得停车时长 不满一小时按一小时计算
        long duration = DateUtil.between(date, chargeEntity.getParkingEndTime(), DateUnit.HOUR) + 1;
        //计算价格
        double amount = priceService.calculationPrice(duration);
        chargeEntity.setParkingEndTime(date);
        chargeEntity.setParkingDuration((int) duration);
        chargeEntity.setChargeAmount(amount);
        //更新停车收费表数据
        baseMapper.updateById(chargeEntity);
        AliPay aliPay = new AliPay();
        aliPay.setTotalAmount(amount);
        aliPay.setTraceNo(String.valueOf(chargeEntity.getChargeId()));
        return aliPay;
    }

//    /**
//     * 更新支付状态 删除车辆信息
//     *
//     * @param chargeID 费用id
//     */
//    @Override
//    @Transactional(rollbackFor = {RuntimeException.class, Exception.class}) //事务
//    public void updateChargePlay(Long chargeID) {
//        ChargeEntity chargeEntity = baseMapper.selectById(chargeID);
//        chargeEntity.setPayment(1);
//        baseMapper.updateById(chargeEntity);
//        try {
//            boolean flag = vehicleService.delVehicle(chargeEntity.getVehiclePlate());
//            if (flag) {
//                return;
//            }
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        } catch (Exception e) {
//            //设置手动回滚
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
//    }

//    /**
//     * 查询是否支付
//     *
//     * @param chargeID 费用id
//     * @return boolean
//     */
//    @Override
//    public boolean selectPayment(Long chargeID) {
//        ChargeEntity chargeEntity = baseMapper.selectById(chargeID);
//        return chargeEntity.getPayment() == 1;
//    }

}
